// 从事件处理池中找当前组件的事件 this.globalEventList.
import { EventBus } from '@/utils/event-bus';
import store from '@/store';
import { GzLog } from './consoleLogButton.js';
export let globalEvent = params => {
	let { compId, eventName, globalEventList, compParams, compName, APPEND_PARAMS = false } = params;
	let eventParamObj = {};
	if (globalEventList && globalEventList.length > 0) {
		let eventConfigArr = globalEventList.filter(event => event.compId == compId && event.eventName == eventName);
		// 一对一和一对多事件
		if (eventConfigArr && eventConfigArr.length > 0) {
			for (const item of eventConfigArr) {
				eventParamObj = { ...item.args, ...compParams, APPEND_PARAMS };
				// 自定义交互传参，采用$$+“变量名形式”
				for (const key in eventParamObj) {
					if (typeof eventParamObj[key] == 'string' && eventParamObj[key].startsWith('$$')) {
						let param = eventParamObj[key].replace(/\$\$/, '');
						eventParamObj[key] = eventParamObj[param];
						delete eventParamObj[param]; // 删除之前的参数$$+A 中的A
					}
				}
				let eventObj = {
					compId,
					targetCompId: item.targetCompId,
					params: eventParamObj,
					eventType: item.eventType,
					eventId: item.eventId,
					linkUrl: item.linkUrl,
					customFn: item.customFn
				};
				GzLog(eventParamObj.APPEND_PARAMS ? 1 : 0, eventParamObj, compName);
				store.commit('event/registerEventParams', eventParamObj);
				EventBus.emit(item.eventId, eventObj);
			}
		}
	}
};
